﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SEVN.Web.Framework.Modules.News
{
    public partial class CateNews0101 : System.Web.UI.UserControl
    {
        WebFrameDataContext db = new WebFrameDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            var data = db.Categories.Where(c => c.IsFontPage==true);
            rptCate.DataSource = data;
            rptCate.DataBind();
        }

        protected void rptCate_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            // Cate News
            HiddenField hdCateID = (HiddenField)e.Item.FindControl("hdCateID");
            Repeater rptChild = (Repeater)e.Item.FindControl("rptChild");
            Repeater rptNewsOther = (Repeater)e.Item.FindControl("rptNewsOther");

            
            if (hdCateID != null && rptChild != null)
            {
                rptChild.DataSource = db.Categories.Where(x => x.CategoryParentID.ToString() == hdCateID.Value);
                rptChild.DataBind();
            }           

            List<Newsletter> listNewsletters = db.Newsletters.Where(n => n.Category.CategoryParentID.ToString() == hdCateID.Value).OrderByDescending(p => p.PostDate).Take(6).ToList();
            if (listNewsletters.Count > 0)
            {
                ImageButton ibtNewsFirst = (ImageButton)e.Item.FindControl("ibtNewsFirst");
                HyperLink hplNewsFirst = (HyperLink)e.Item.FindControl("hplNewsFirst");
                Label lblSubTitle = (Label)e.Item.FindControl("lblSubTitle");
                Label lblPublishDateNews = (Label)e.Item.FindControl("lblPublishDateNews");
                Image imgNews = (Image)e.Item.FindControl("imgNews");

                ibtNewsFirst.Visible = true;
                ibtNewsFirst.ImageUrl = string.IsNullOrEmpty(listNewsletters[0].Image) ? "~/Images/Design/Normal.gif" : listNewsletters[0].Image;
                ibtNewsFirst.PostBackUrl = "~/Newsletter.aspx?id=" + listNewsletters[0].NewsID;
                hplNewsFirst.NavigateUrl = "~/Newsletter.aspx?id=" + listNewsletters[0].NewsID;
                hplNewsFirst.Text = listNewsletters[0].Title;
                hplNewsFirst.ToolTip = listNewsletters[0].Title;
                lblSubTitle.Text = listNewsletters[0].SubTitle;

                //lblPublishDateNews.Text = "(" + Utility.GetFullDateTime(listNewsletters[0].PublishDate.Value)+")";
                lblPublishDateNews.Text = "Đăng ngày: " + Convert.ToDateTime(listNewsletters[0].PostDate.ToString()).ToString("dd/MM/yyyy hh:mm tt");
                bool isNews = DateTime.Compare(listNewsletters[0].PostDate.Value.AddDays(14), DateTime.Now) > 0 ? true : false;
                imgNews.Visible = isNews;
            }

            if (rptNewsOther != null)
            {
                var newsOther = (from n in listNewsletters
                                 select new { n.NewsID, n.Title, n.SubTitle, IsNew = DateTime.Compare(n.PostDate.Value.AddDays(14), DateTime.Now) > 0 ? true : false });
                rptNewsOther.DataSource = newsOther.Skip(1).Take(5);
                rptNewsOther.DataBind();
            }
        }
    }
}